let iponeReg =
  /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
let emailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
let userNameReg = /\w/;
let passwordReg = /.{6,}/;

let phoneFlog = false;
let userNameFlog = false;
let passwordFlog = false;
let password2Flog = false;
let checkedFlog = false;

let iptArr = document.querySelectorAll(".el-input__inner");

function reg() {
  console.log("监听监听");
  console.log(
    phoneFlog,
    userNameFlog,
    passwordFlog,
    password2Flog,
    checkedFlog
  );
  if (
    phoneFlog &&
    userNameFlog &&
    passwordFlog &&
    password2Flog &&
    checkedFlog
  ) {
    console.log("可以注册");
    $(".el-button").removeAttr("disabled").css({
      background: "red",
    });
    $(".el-button").click(function () {
      localStorage.setItem("username", iptArr[1].value);
      $.ajax({
        type: "get",
        url: "http://jx.xuzhixiang.top/ap/api/reg.php",
        data: {
          username: iptArr[1].value,
          password: iptArr[2].value,
        },
        success: function (res) {
          console.log(res);
          if (res.code == 1) {
            layui.use('layer', function () {
              var layer = layui.layer;
              layer.msg('"注册成功 即将跳转到登录页...');

            });
            setTimeout(() => {
              location.href = "login.html";
            }, 1800);

          }
        },
      });
    });
  } else {
    $(".el-button").attr("disabled", "disabled").css({
      background: "#ccc",
    });
  }
}
iptArr[0].oninput = function () {
  console.log(this.value);
  if (iponeReg.test(this.value) || emailReg.test(this.value)) {
    phoneFlog = true;
    this.nextElementSibling.innerHTML = "格式正确";
    this.nextElementSibling.style.color = "green";
    phoneFlog = true;
  } else {
    console.log("不通过");
    phoneFlog = false;
    this.nextElementSibling.innerHTML = "请检查格式";
    this.nextElementSibling.style.color = "red";
    //    phoneFlog = false;
  }
  console.log(phoneFlog);
  reg();
};
iptArr[1].oninput = function () {
  console.log(this.value);
  if (userNameReg.test(this.value)) {
    let checkUsernameAPI = "http://jx.xuzhixiang.top/ap/api/checkname.php";
    axios
      .get(checkUsernameAPI, {
        params: {
          username: this.value,
        },
      })
      .then((r) => {
        if (r.data.code == 1) {
          userNameFlog = true;
          this.nextElementSibling.innerHTML = "用户名可用";
          this.nextElementSibling.style.color = "green";
        } else {
          userNameFlog = false;
          this.nextElementSibling.innerHTML = "用户名重复";
          this.nextElementSibling.style.color = "red";
        }
        console.log(userNameFlog);
        reg();
      });
  } else {
    console.log("不通过");
    this.nextElementSibling.innerHTML = "请检查格式";
    this.nextElementSibling.style.color = "red";
    userNameFlog = false;
  }
  reg();
};

iptArr[2].oninput = function () {
  console.log(this.value);
  if (passwordReg.test(this.value)) {
    this.nextElementSibling.innerHTML = "格式正确";
    this.nextElementSibling.style.color = "green";
    passwordFlog = true;
  } else {
    //    console.log("不通过");
    this.nextElementSibling.innerHTML = "请检查格式";
    this.nextElementSibling.style.color = "red";
    passwordFlog = false;
  }
  reg();
  if (iptArr[3].value == iptArr[2].value) {
    iptArr[3].nextElementSibling.innerHTML = "格式正确";
    iptArr[3].nextElementSibling.style.color = "green";
    password2Flog = true;
  } else {
    //    console.log("不通过");
    iptArr[3].nextElementSibling.innerHTML = "密码不一致";
    iptArr[3].nextElementSibling.style.color = "red";
    password2Flog = false;
  }
  reg();
};
iptArr[3].oninput = function () {
  console.log(this.value);
  if (iptArr[3].value == iptArr[2].value) {
    this.nextElementSibling.innerHTML = "格式正确";
    this.nextElementSibling.style.color = "green";
    password2Flog = true;
  } else {
    //    console.log("不通过");
    this.nextElementSibling.innerHTML = "密码不一致";
    this.nextElementSibling.style.color = "red";
    password2Flog = false;
  }
  reg();
};
$(".el-checkbox__original").change(function () {
  // console.log(this.checked);
  if (this.checked) {
    checkedFlog = true;
  } else {
    checkedFlog = false;
  }
  reg();
});